<script setup lang="ts">
import { ref, watch } from 'vue'
import { ArrowRight } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'
import { getCategoryByIdApi } from '@/apis/category'
import type { CategoryByIdType } from '@/apis/components/categoryType'
import HomeBanner from '../Home/components/HomeBanner.vue'
import GoodsItem from '../Home/components/GoodsItem.vue'

const route = useRoute()

const categoryListById = ref<CategoryByIdType>()

const getCategoryById = async () => {
  const id = route.path.slice(10)
  const res = await getCategoryByIdApi(id)
  categoryListById.value = res.data.result
}
watch(
  () => route.path,
  () => {
    getCategoryById()
  },
  { immediate: true },
)
</script>

<template>
  <div class="box">
    <div class="breadcrumb-box">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{ categoryListById?.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="banber-box">
      <HomeBanner :id="'2'"></HomeBanner>
    </div>
    <div class="allCategory">
      <p class="title">全部分类</p>
      <ul>
        <li v-for="item in categoryListById?.children" :key="item.id">
          <RouterLink :to="`/category/subcategory/${item.id}`">
            <img :src="item.picture" alt="" />
            <p>{{ item.name }}</p>
          </RouterLink>
        </li>
      </ul>
    </div>
    <div class="among-box" v-for="item in categoryListById?.children" :key="item.id">
      <p>{{ item.name }}</p>
      <ul>
        <li v-for="value in item.goods" :key="value.id">
          <GoodsItem :goods="value"></GoodsItem>
        </li>
      </ul>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.breadcrumb-box {
  width: 1100px;
  height: 30px;
  margin-left: 20px;
  .el-breadcrumb {
    font-size: 18px;
  }
}
.allCategory {
  width: 100%;
  margin: 0 auto;
  text-align: center;
  .title {
    width: 100%;
    height: 30px;
    line-height: 30px;
    font-size: 30px;
  }
  ul {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    margin-top: 10px;
    li {
      width: 100px;
      height: 150px;
      list-style: none;
      a {
        color: black;
        text-decoration: none;
        &:hover {
          color: aquamarine;
        }
      }

      img {
        width: 100%;
        height: 100px;
        background-color: rgba(0, 0, 0, 0.11);
      }
      p {
        font-size: 14px;
        line-height: 50px;
      }
    }
  }
}
.among-box {
  width: 1100px;
  margin: 0 auto;
  text-align: center;
  p {
    width: 100%;
    height: 30px;
    line-height: 30px;
    font-size: 30px;
  }
  ul {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    margin-top: 20px;
    li {
      list-style: none;
    }
  }
}
</style>
